// pages/topics/topics.js
const app = getApp()
//const user = JSON.parse(wx.getStorageSync("user"));
const base_url = app.globalData.base_url;
var util = require('../../utils/util.js');
var WxParse = require('../../common/wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    topicArr: [],
    page: {//话题详情
      p: 0,
      psize: 10
    },
    topicArr: [],
    aas: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.init();
    wx.setNavigationBarTitle({
      title: "话题收藏"
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onReachBottom() {
    wx.showLoading({
      title: '加载中',
      success: () => {
        this.setData({
          'page.p': this.data.page.p + 1
        })
        this.init()

      }
    })
  },
  onPullDownRefresh() {
    this.setData({
      'page.p': 0
    })
    let refresh = true
    this.init(refresh)
  },
  init: function (refresh = false) {
    var that = this;
    wx.showLoading({
      title: '加载中',
      success: () => {
        wx.request({
          url: base_url + '/Users/getFollowTopic',
          data: {
          //  'is_all': 1,
            'user_token': JSON.parse(wx.getStorageSync("user")).user_token,
            'p': this.data.page.p,
            'psize': this.data.page.psize
          },
          method: 'POST',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: res => {
            wx.hideLoading();
            if (res.data.success == '1' && res.data.data.length != 0) {
              let arr = refresh ? res.data.data : this.data.topicArr.concat(res.data.data)
              // let newArr=[]
              // for(let val of arr){
              //   newArr.push(val.topic_content)
              // }
              for (let i = 0; i < arr.length; i++) {
                WxParse.wxParse('reply' + i, 'html', arr[i].topic_content, that);
                if (i === arr.length - 1) {
                  WxParse.wxParseTemArray("topicArrs", 'reply', arr.length, that)
                }
                //WxParse.wxParseTemArray("topicArr", 'reply', val.topic_content, that)
              }
              this.setData({
                topicArr: arr,
              })
            } else {
              wx.showToast({
                title: '暂无更多数据',
                icon: 'none',
                duration: 2000
              })
            }

            refresh == true && wx.stopPullDownRefresh()
          }
        })
      }
    })

  },
  topicDetail: function (e) {
    wx.navigateTo({
      url: '../topicDetail/topicDetail?id=' + e.currentTarget.dataset.topicid
    })
  }
})